package com.android.phpclient;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;

import com.google.gson.Gson;

import Clases.AlumnoDatos;
import Clases.Datos_Profesor;
import Clases.Grupo;
import Clases.Materias;
import Clases.Unidades;
import Clases.calificacion;
import android.util.Log;

public class ServerAccess {

	
	public static String convertStreamToString(InputStream is) 
    {
    	BufferedReader reader = new BufferedReader(new InputStreamReader(is));
    	StringBuilder sb = new StringBuilder();
    	
    	String line = null;
    	try {
    		while ((line = reader.readLine()) != null) {
    			sb.append(line + "\n");
    		}
    	} 
    	catch (IOException e) {
    		Log.e("PHP Client","Error : "+e.getMessage());
    	} 
    	finally {
    		try {
    			is.close();
    		} catch (IOException e1) {
    			Log.e("PHP Client","Error : "+e1.getMessage());
    		}
    	}
    	return sb.toString();
    }

	
	public Object getResponseObject(ArrayList<NameValuePair> Parameters,Class c)
	{
		try{
			
			HttpClient httpclient = new DefaultHttpClient();
			
	
			HttpPost httppost = new HttpPost(Constants.SERVICE_URL);
			
			
			httppost.setEntity(new UrlEncodedFormEntity(Parameters));
			
	
			HttpResponse response = httpclient.execute(httppost);
		
			HttpEntity entity = response.getEntity();
		
			InputStream stream=entity.getContent();
			
	        String result= convertStreamToString(stream);
	        
	        Gson gson=new Gson();
	        
			Object responseObject=gson.fromJson(result, c);
		
	        return responseObject;
		}catch(Exception e){
			Log.e("PHP Client", "Error in http connection"+e.toString());
			return null;
		}
	}
	
	
	public int getUserID(String Username,String Password)
	{
		
		ArrayList<NameValuePair> parameters=new ArrayList<NameValuePair>();
		
		parameters.add(new BasicNameValuePair("method","verifyLogin"));
		
		parameters.add(new BasicNameValuePair("username",Username));
		
		parameters.add(new BasicNameValuePair("password",Password));
		
		LoginResult o= (LoginResult)getResponseObject(parameters,LoginResult.class);
		return Integer.parseInt(o.result);
	}
	
	public String Tipo_Usuario (String Username,String Password)
	{
		
		ArrayList<NameValuePair> parameters=new ArrayList<NameValuePair>();
		
	
		parameters.add(new BasicNameValuePair("method","verificar_tipo_usuario"));
		
		parameters.add(new BasicNameValuePair("username",Username));

		parameters.add(new BasicNameValuePair("password",Password));
	
		LoginResult o= (LoginResult)getResponseObject(parameters,LoginResult.class);
		return o.result;
	}
	
	

	
	
	public Employee[] getEmployees(String idU,String idM,String idG)
	{
		
		ArrayList<NameValuePair> parameters=new ArrayList<NameValuePair>();
		
		parameters.add(new BasicNameValuePair("method","getEmployees"));
		parameters.add(new BasicNameValuePair("unidadID",String.valueOf(idU)));
		parameters.add(new BasicNameValuePair("materiaID",String.valueOf(idM)));
		parameters.add(new BasicNameValuePair("grupoID",String.valueOf(idG)));
	
		Employee[] o= (Employee[])getResponseObject(parameters,Employee[].class);
		return o;
	}
	

	
	public Grupo[] grupo(String id)
	{

		ArrayList<NameValuePair> parameters=new ArrayList<NameValuePair>();

		parameters.add(new BasicNameValuePair("method","grupos"));
		parameters.add(new BasicNameValuePair("id",id));
		
	
		Grupo[] o= (Grupo[])getResponseObject(parameters,Grupo[].class);
		return o;
	}
	
	
	public Grupo[] grupoid(String id)
	{

		ArrayList<NameValuePair> parameters=new ArrayList<NameValuePair>();

		parameters.add(new BasicNameValuePair("method","Grupo_ID"));
		//parameters.add(new BasicNameValuePair("id",String.valueOf(id)));
		parameters.add(new BasicNameValuePair("id",id));
	
		Grupo[] o= (Grupo[])getResponseObject(parameters,Grupo[].class);
		return o;
	}
	
	public AlumnoDatos[] DatosAlumno(String id)
	{

		ArrayList<NameValuePair> parameters=new ArrayList<NameValuePair>();

	
		parameters.add(new BasicNameValuePair("method","Datos_Alumno"));
		parameters.add(new BasicNameValuePair("id",String.valueOf(id)));
		
	
		AlumnoDatos[] o= (AlumnoDatos[])getResponseObject(parameters,AlumnoDatos[].class);
		return o;
	}
	
	
	public Unidades[] unidades()
	{

		ArrayList<NameValuePair> parameters=new ArrayList<NameValuePair>();

		
		parameters.add(new BasicNameValuePair("method","unidad_clase"));

	
		Unidades[] o= (Unidades[])getResponseObject(parameters,Unidades[].class);
		return o;
	}
	
	


	
	public Materias[] MATERIA(String id)
	{

		ArrayList<NameValuePair> parameters=new ArrayList<NameValuePair>();

		parameters.add(new BasicNameValuePair("method","Materia_Profesor"));
		parameters.add(new BasicNameValuePair("id",String.valueOf(id)));
		
	
		Materias[] o= (Materias[])getResponseObject(parameters,Materias[].class);
		return o;
	}
	
	public Materias[] MATERIAID(String id)
	{

		ArrayList<NameValuePair> parameters=new ArrayList<NameValuePair>();

		parameters.add(new BasicNameValuePair("method","Materia_Profesor_ID"));
		parameters.add(new BasicNameValuePair("id",String.valueOf(id)));
		
	
		Materias[] o= (Materias[])getResponseObject(parameters,Materias[].class);
		return o;
	}
	
	
	public Materias[] Materias_Alumnos(String id)
	{

		ArrayList<NameValuePair> parameters=new ArrayList<NameValuePair>();

		parameters.add(new BasicNameValuePair("method","Materia_Alumno"));
		parameters.add(new BasicNameValuePair("id",String.valueOf(id)));
		
	
		Materias[] o= (Materias[])getResponseObject(parameters,Materias[].class);
		return o;
	}
	
	
	public Materias[] Materias_AlumnosID(String id)
	{

		ArrayList<NameValuePair> parameters=new ArrayList<NameValuePair>();

		parameters.add(new BasicNameValuePair("method","Materia_Alumno_id"));
		parameters.add(new BasicNameValuePair("id",String.valueOf(id)));
		
	
		Materias[] o= (Materias[])getResponseObject(parameters,Materias[].class);
		return o;
	
	}
	
	
	public Materias[] Materias_Cuatrimestre()
	{

		ArrayList<NameValuePair> parameters=new ArrayList<NameValuePair>();

		parameters.add(new BasicNameValuePair("method","Materia_Cuatrimestre"));
	
		
	
		Materias[] o= (Materias[])getResponseObject(parameters,Materias[].class);
		return o;
	
	}
	
	
	public String Ingresar_calificacion(String calificacion,int numero,String unidades_id,String tipos,String materia_id,String matricula)
	{

		ArrayList<NameValuePair> parameters=new ArrayList<NameValuePair>();

		parameters.add(new BasicNameValuePair("method","inser_into2"));
		parameters.add(new BasicNameValuePair("calificacion",String.valueOf(calificacion)));
		parameters.add(new BasicNameValuePair("numero",String.valueOf(numero)));
		parameters.add(new BasicNameValuePair("unidades_id",String.valueOf(unidades_id)));
		parameters.add(new BasicNameValuePair("tipos",String.valueOf(tipos)));
		parameters.add(new BasicNameValuePair("materia_id",String.valueOf(materia_id)));
		parameters.add(new BasicNameValuePair("matricula",String.valueOf(matricula)));
		
	
		calificacion o= (calificacion)getResponseObject(parameters,calificacion.class);
		return o.message;
	}

	
	public String modificar_calificacion(String calificacion,int numero,String unidades_id,String tipos,String materia_id,String matricula)
	{

		ArrayList<NameValuePair> parameters=new ArrayList<NameValuePair>();

		parameters.add(new BasicNameValuePair("method","ModificarCalificacion"));
		parameters.add(new BasicNameValuePair("calificacion",String.valueOf(calificacion)));
		parameters.add(new BasicNameValuePair("numero",String.valueOf(numero)));
		parameters.add(new BasicNameValuePair("unidades_id",String.valueOf(unidades_id)));
		parameters.add(new BasicNameValuePair("tipos",String.valueOf(tipos)));
		parameters.add(new BasicNameValuePair("materia_id",String.valueOf(materia_id)));
		parameters.add(new BasicNameValuePair("matricula",String.valueOf(matricula)));
		
	
		calificacion o= (calificacion)getResponseObject(parameters,calificacion.class);
		return o.message;
	}
	
	public String  Paterno(String id)
	{
		
		ArrayList<NameValuePair> parameters=new ArrayList<NameValuePair>();

		parameters.add(new BasicNameValuePair("method","Nombre_Profesor"));
		parameters.add(new BasicNameValuePair("id",id));
		
		
		Datos_Profesor o= (Datos_Profesor)getResponseObject(parameters,Datos_Profesor.class);
		return o.nombre;
	}
	
	
	public String  MatriaProfesorID(String id,String idGrupo)
	{
		
		ArrayList<NameValuePair> parameters=new ArrayList<NameValuePair>();

		parameters.add(new BasicNameValuePair("method","Materia_Profesor_Grupo"));
		parameters.add(new BasicNameValuePair("id",id));
		parameters.add(new BasicNameValuePair("grupo_id",idGrupo));
		
		Datos_Profesor o= (Datos_Profesor)getResponseObject(parameters,Datos_Profesor.class);
		return o.nombre;
	}
	
	
	public String  ReturnNombreAlum(String id)
	{
		
		ArrayList<NameValuePair> parameters=new ArrayList<NameValuePair>();

		parameters.add(new BasicNameValuePair("method","BusquedaAlumnoN"));
		parameters.add(new BasicNameValuePair("id",id));
		
		
		Datos_Profesor o= (Datos_Profesor)getResponseObject(parameters,Datos_Profesor.class);
		return o.nombre;
	}
	
	public String  ReturnMatriculaAlum(String id)
	{
		
		ArrayList<NameValuePair> parameters=new ArrayList<NameValuePair>();

		parameters.add(new BasicNameValuePair("method","ReturnMatriculaAl"));
		parameters.add(new BasicNameValuePair("id",id));
		
		
		Datos_Profesor o= (Datos_Profesor)getResponseObject(parameters,Datos_Profesor.class);
		return o.nombre;
	}
	
	public String  CalificacionMateria(String idMateria,String idUnidad,String matricula)
	{
		
		ArrayList<NameValuePair> parameters=new ArrayList<NameValuePair>();

		parameters.add(new BasicNameValuePair("method","CalificacionAlumno"));
		parameters.add(new BasicNameValuePair("id",String.valueOf(idMateria)));
		parameters.add(new BasicNameValuePair("idUnidad",String.valueOf(idUnidad)));
		parameters.add(new BasicNameValuePair("matricula",String.valueOf(matricula)));
		
		Datos_Profesor o= (Datos_Profesor)getResponseObject(parameters,Datos_Profesor.class);
		return o.nombre;
	}
}
